/*
 *  CENodeRGBA.cpp
 *  LID Library
 *
 *  Created by Alexander Trutchenko on 09/11/11.
 *  Copyright 2011 League of Independent Developers. All rights reserved.
 *
 */

#include "cocos2dx_extentions/nodes/CENodeRGBA.h"

using namespace cocos2d;

namespace cocosext {

/////////////////////////////////////////////////////
//
/** CERGBAProtocolRecursive 
* CERGBAProtocolRecursive set color/opacity to all its children recursivly
*/
/////////////////////////////////////////////////////

void CERGBAProtocolRecursive::setColorRecursive(CCNode* node, const ccColor3B& color)
{
	CCRGBAProtocol *pRGBAProtocol = dynamic_cast<CCRGBAProtocol*>(node);
	if (pRGBAProtocol != NULL && dynamic_cast<CERGBAProtocolRecursive*>(node) == NULL)
		pRGBAProtocol->setColor(color);

	if (node->getChildren() == NULL)
		return;
	
	for (unsigned int i = 0; i < node->getChildren()->count(); ++i)
		setColorRecursive((CCNode*)node->getChildren()->objectAtIndex(i), color);
}

void CERGBAProtocolRecursive::setOpacityRecursive(CCNode* node, GLubyte opacity)
{
	CCRGBAProtocol *pRGBAProtocol = dynamic_cast<CCRGBAProtocol*>(node);
	if (pRGBAProtocol != NULL && dynamic_cast<CERGBAProtocolRecursive*>(node) == NULL)
		pRGBAProtocol->setOpacity(opacity);
	
	if (node->getChildren() == NULL)
		return;

	for (unsigned int i = 0; i < node->getChildren()->count(); ++i)
		setOpacityRecursive((CCNode*)node->getChildren()->objectAtIndex(i), opacity);
}

void CERGBAProtocolRecursive::setIsOpacityModifyRGBRecursive(CCNode* node, bool bValue)
{
	CCRGBAProtocol *pRGBAProtocol = dynamic_cast<CCRGBAProtocol*>(node);
	if (pRGBAProtocol != NULL && dynamic_cast<CERGBAProtocolRecursive*>(node) == NULL)
		pRGBAProtocol->setIsOpacityModifyRGB(bValue);
	
	if (node->getChildren() == NULL)
		return;

	for (unsigned int i = 0; i < node->getChildren()->count(); ++i)
		setIsOpacityModifyRGBRecursive((CCNode*)node->getChildren()->objectAtIndex(i), bValue);
}


/////////////////////////////////////////////////////
//
/** CENodeRGBA 
* NodeRGBA control. Returns it's values to registered delegates
*/
/////////////////////////////////////////////////////

CENodeRGBA* CENodeRGBA::node()
{
	CENodeRGBA *pRet = new CENodeRGBA();
    CCAssert(pRet != NULL, NULL);
    if (pRet->init())
    {
        pRet->autorelease();
        return pRet;
    }
    else
    {
        CC_SAFE_DELETE(pRet)
        return NULL;
    }
}

/////////////////////////////////////////////////////
// CCRGBAProtocol


void CENodeRGBA::setColor(const ccColor3B& color)
{
	_color = color;
	setColorRecursive(this, _color);
}

const ccColor3B& CENodeRGBA::getColor(void)
{
	return _color;
}

GLubyte CENodeRGBA::getOpacity(void)
{
	return _opacity;
}

void CENodeRGBA::setOpacity(GLubyte opacity)
{
	_opacity = opacity;
	setOpacityRecursive(this, _opacity);
}

void CENodeRGBA::setIsOpacityModifyRGB(bool bValue)
{
	_bOpacityModifyRGB = bValue;
	setIsOpacityModifyRGBRecursive(this, bValue);
}

bool CENodeRGBA::getIsOpacityModifyRGB(void)
{
	return _bOpacityModifyRGB;
}

}; // end namespace cocosext

